home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / pcl4c60.zip / MODEM_IO.C < prev    next >
Text File  |  1996-10-10  |  7KB  |  318 lines

  1. /*
  2. **   -- MODEM_IO.C --
  3. **
  4. **  Define USE_WIN_IO to 1 if using the WIN_IO window tile
  5. **  routines, else set USE_WIN_IO to 0.
  6. */
  7.  
  8. #define USE_WIN_IO 0
  9.  
  10. /**********************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <conio.h>
  17. #include "pcl4c.h"
  18. #include "modem_io.h"
  19.  
  20. #if USE_WIN_IO
  21. #include "win_io.h"
  22. #endif
  23.  
  24. #define FALSE 0
  25. #define TRUE !FALSE
  26. #define ONE_SECOND 18
  27. #define CR 0x0d
  28.  
  29. /*** globals ***/
  30.  
  31. static int  Debug = 0;
  32. static int  LastPort;         /* last port referenced */
  33. static char MatchString[80];  /* ModemWaitFor() match string */
  34. static int  MatchLength= 0;   /* string length */
  35. static int  MatchCount = 0;   /* # sub-strings */
  36. static struct
  37.   {char *Start;               /* ptr to 1st char of string */
  38.    char *Ptr;                 /* working ptr */
  39.   } MatchList[10];
  40.  
  41. /*** PRIVATE functions ***/
  42.  
  43. static int BreakTest(void)
  44. {/* User BREAK ? */
  45.  if(SioBrkKey()||kbhit())
  46.     {
  47. #if USE_WIN_IO
  48.      WinPutString(SCR_WIN,"User BREAK\n");
  49. #else
  50.      printf("User BREAK\n");
  51. #endif
  52.      return(TRUE);
  53.     }
  54.  return(FALSE);
  55. }
  56.  
  57. void MatchInit(char *S)
  58. {int  i;
  59.  char C;
  60.  char *Ptr;
  61.  MatchCount = 0;
  62.  strncpy(MatchString,S,80);
  63.  MatchLength = strlen(MatchString);
  64.  Ptr = MatchString;
  65.  MatchList[MatchCount].Start = Ptr;
  66.  MatchList[MatchCount++].Ptr = Ptr;
  67.  while(*Ptr)
  68.    {if(*Ptr=='|')
  69.       {/* mark start of next string */
  70.        MatchList[MatchCount].Start = Ptr + 1;
  71.        MatchList[MatchCount++].Ptr = Ptr + 1;
  72.       }
  73.     Ptr++;
  74.    }
  75. }
  76.  
  77. void MatchUpper(void)
  78. {int i;
  79.  char *Ptr;
  80.  Ptr = MatchString;
  81.  for(i=0;i<MatchLength;i++)
  82.    {*Ptr = toupper(*Ptr);
  83.     Ptr++;
  84.    }
  85. }
  86.  
  87. int MatchChar(char C)
  88. {int  i;
  89.  char *Ptr;
  90.  char *Start;
  91.  /* consider each string in turn */
  92.  for(i=0;i<MatchCount;i++)
  93.    {Ptr = MatchList[i].Ptr;
  94.     Start = MatchList[i].Start;
  95.     if(*Ptr==C)
  96.       {/* char C matches */
  97.        Ptr++;
  98.        if((*Ptr=='|')||(*Ptr=='\0'))
  99.          {MatchList[i].Ptr = Start;
  100.           return i;
  101.          }
  102.        else MatchList[i].Ptr = Ptr;
  103.       }
  104.     else
  105.       {/* char C does NOT match */
  106.        MatchList[i].Ptr = Start;
  107.        /* look again if was not 1st char  */
  108.        if(Ptr!=Start) i--;
  109.       }
  110.    }
  111.  return -1;
  112. }
  113.  
  114. /*** PUBLIC functions ***/
  115.  
  116. /* echos incoming to screen */
  117.  
  118. void ModemEcho(int Port,int Tics)
  119. {int rc;
  120.  long Time;
  121.  Time = SioTimer();
  122.  while(SioTimer() < Time+(long)Tics)
  123.    {
  124. #if USE_WIN_IO
  125.     rc = CharGet(Port,1);
  126.     if(rc>=0) WinPutChar(SCR_WIN,(char)rc);
  127. #else
  128.     rc = SioGetc(Port,1);
  129.     if(rc>=0) putch((char)rc);
  130. #endif
  131.    }
  132. }
  133.  
  134. /* send string to modem & get echo */
  135.  
  136. int ModemSendTo(
  137.   int  Port,       /* port to talk to */
  138.   int  Pace,       /* inter-char delay */
  139.   char *String)    /* string to send to modem */
  140. {int i, rc;
  141.  char c;
  142.  int Code;
  143.  long Time;
  144.  char Temp[80];
  145.  if(Debug)
  146.    {sprintf(Temp," [Sending '%s'] ",String);
  147. #if USE_WIN_IO
  148.     WinPutString(SCR_WIN,Temp);
  149. #else
  150.     printf("%s\n",Temp);
  151. #endif
  152.    }
  153.  for(i=0;i<strlen(String);)
  154.     {/* User BREAK ? */
  155.      if(BreakTest()) return(FALSE);
  156.      /* delay <Pace> tics */
  157.      if(Pace>0) SioDelay(Pace);
  158.      /* fetch character */
  159.      c = String[i++];
  160.      switch(c)
  161.         {case '^':
  162.             /* next char is control char */
  163.             c = String[i++] - '@';
  164.             break;
  165.          case '!':
  166.             /* replace ! with carriage return */
  167.             c = CR;
  168.             break;
  169.          case '~':
  170.             /* delay 1/2 second */
  171.             SioDelay(ONE_SECOND/2);
  172.             c = ' ';
  173.             break;
  174.          case ' ':
  175.             /* delay 1/4 second */
  176.             SioDelay(ONE_SECOND/4);
  177.             c = ' ';
  178.             break;
  179.         } /* end switch */
  180.      /* transmit as 7 bit ASCII character */
  181. #if USE_WIN_IO
  182.      CharPut(Port,(char)(0x7f & c));
  183. #else
  184.      SioPutc(Port,(char)(0x7f & c));
  185. #endif
  186.     }
  187.  return(TRUE);
  188. } /* end SendTo */
  189.  
  190. /* wait for 'String' */
  191.  
  192. /* NOTES:
  193. **  (1)  Will return NULL if no match, else '0' if 1st, '2' if 2nd, etc.
  194. **       where String = "<1st substr>|<2nd substr>| ..."
  195. **  (2)  Example call: ModemWaitFor(COM1,180,FALSE,"more ?|Menu:");
  196. */
  197.  
  198. char ModemWaitFor(
  199.   int  Port,       /* Port to talk to */
  200.   int  Tics,       /* wait in tics for string */
  201.   int  CaseFlag,   /* TRUE = case sensitive compares */
  202.   char *String)    /* string to wait for */
  203. {int i, k;
  204.  int rc;
  205.  char C;
  206.  int Code;
  207.  long Time;
  208.  char Temp[80];
  209.  /* wait for string */
  210.  Time = SioTimer();
  211.  MatchInit(String);
  212.  if(!CaseFlag) MatchUpper();
  213.  if(Debug)
  214.    {sprintf(Temp," [Awaiting '%s'] ",String);
  215. #if USE_WIN_IO
  216.     WinPutString(SCR_WIN,Temp);
  217. #else
  218.     printf("%s\n",Temp);
  219. #endif
  220.    }
  221.  while(SioTimer()<(Time+(long)Tics))
  222.    {/* User BREAK ? */
  223.     if(BreakTest()) return(FALSE);
  224.     /* wait for next character */
  225. #if USE_WIN_IO
  226.     Code = CharGet(Port,1);
  227. #else
  228.     Code = SioGetc(Port,1);
  229. #endif
  230.     if(Code<-1) return(FALSE);
  231.     if(Code>=0)
  232.       {/* echo char */
  233. #if USE_WIN_IO
  234.        WinPutChar(SCR_WIN,(char)Code);
  235. #else
  236.        printf("%c",(char)Code);
  237. #endif
  238.        /* case sensitive ? */
  239.        if(CaseFlag) C = (char)Code;
  240.        else C = toupper( (char)Code );
  241.        /* does char match ? */
  242.        rc = MatchChar(C);
  243.        if(rc>=0) return ('0' + rc);
  244.       }
  245.    } /* end for(i) */
  246.  return 0;
  247. }
  248.  
  249. /* enter command state */
  250.  
  251. /* NOTE: assumes escape char = '+' & guard time = 1 sec */
  252.  
  253. void ModemCmdState(int Port)
  254. {int  i;
  255.  char Temp[80];
  256.  /* delay a bit over 1 second */
  257.  SioDelay(ONE_SECOND+ONE_SECOND/4);
  258.  /* send Escape Code exactly 3 times */
  259.  for(i=0;i<3;i++)
  260.     {
  261. #if USE_WIN_IO
  262.      CharPut(Port,'+');
  263. #else
  264.      SioPutc(Port,'+');
  265. #endif
  266.      SioDelay(ONE_SECOND/4);
  267.     }
  268.  /* delay again */
  269.  SioDelay(ONE_SECOND+ONE_SECOND/4);
  270. } /* end ModemCmdState */
  271.  
  272. /* hangup phone (in command state) */
  273.  
  274. void ModemHangup(int Port)
  275. {/* enter command state */
  276.  ModemCmdState(Port);
  277.  /* hangup ! */
  278.  ModemSendTo(Port,4,"!AT!");
  279.  ModemEcho(Port,10);
  280.  ModemSendTo(Port,4,"ATH0!");
  281. } /* end Hangup */
  282.  
  283. /* wait for continuous quiet (no incoming serial data) */
  284.  
  285. int ModemQuiet(
  286.   int  Port,       /* Port to talk to */
  287.   int  Tics)       /* # tics quiet required */
  288. {int i;
  289.  int Code;
  290.  long CharTime;
  291.  /* set up */
  292.  CharTime = SioTimer();
  293.  while(1)
  294.    {/* User BREAK ? */
  295.     if(BreakTest()) return(FALSE);
  296.     /* wait for next character */
  297. #if USE_WIN_IO
  298.     Code = CharGet(Port,1);
  299. #else
  300.     Code = SioGetc(Port,1);
  301. #endif
  302.     if(Code<-1) return(FALSE);
  303.     if(Code>=0)
  304.       {SioRxClear(Port);
  305.        /* reset timer */
  306.        CharTime = SioTimer();
  307.       }
  308.     else
  309.       {/* Code==-1 (timed out) */
  310.        if(SioTimer() >= CharTime+(long)Tics) return TRUE;
  311.       }
  312.    } /* end while */
  313. }
  314.  
  315. void ModemDebug(void)
  316. {Debug = TRUE;
  317. }
  318.